package commEnv

import (
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/Map"
	"os"
	"strconv"
)

var (
	args    *appArgs
	Conf    *confLoader
	Service *service
)

type service struct {
	Port        int    // 服务端口
	WorkDir     string // 工作目录
	RedisPrefix string // 路由前缀
	RedisWriter bool   // 从配置文件中写入路由到redis
	CertFile    string
	KeyFile     string
	EdgeId      string
	Topic       struct {
		Request string
		Respond string
	}
	Mqtt map[string]interface{}
}

func init() {
	args = &appArgs{}
	Conf = &confLoader{}
	Service = &service{}

	// 从命令行读取参数
	args.initialize()

	Port, _ := strconv.ParseInt(Map.GetString(args.params, "port", ""), 10, 32)
	Service.Port = int(Port)

	Service.RedisPrefix = Map.GetString(args.params, "redis.prefix", "")
	RedisWriter, _ := strconv.ParseBool(Map.GetString(args.params, "redis.writer", "false"))
	Service.RedisWriter = RedisWriter

	Service.CertFile = Map.GetString(args.params, "https.certFile", "")
	Service.KeyFile = Map.GetString(args.params, "https.keyFile", "")

	workPath := Map.GetString(args.params, "workPath", "")
	if workPath == "" {
		wd, _ := os.Getwd()
		Service.WorkDir = wd
	} else {
		Service.WorkDir = workPath
	}
}
